<template>
  <div class="table-ctrl-body">
    <!--按钮组-->
    <div class="Btns-ctrl-body">
      <div class="search-ctrl f-l"  style="display: none">
        <form style="height: 100%;">
          <input placeholder="搜索关键字" class="search-text">
          <button class="search-btn btn btn-info" type="button">搜索</button>
        </form>
      </div>
      <div class="btns f-r">
        <button type="button" class="btn btn-info" @click="menuAddFn">新增</button>
        <button type="button" class="btn btn-warning" @click="menuRevampFn">修改</button>
        <button type="button" class="btn btn-danger" @click="menuDeleteFn">删除</button>
      </div>
    </div>
    <!--按钮组闭合-->
    <!--表格组-->
    <div class="table-ctrl">
      <!--表格头-->
      <div class="table-header">
        <table class="table-default">
          <thead>
          <tr>
            <th v-for="ths in tHeader">
              {{ths.value}}
            </th>
          </tr>
          </thead>
        </table>
      </div>
      <!--表格中部-->
      <div class="table-overflowX">
        <table class="table-default">
          <tbody>
          <tr v-for="(trs,index) in tableRow.data" :class="{active: index === isActive}" @click="selectJurisdiction(trs,index)">
            <td v-for="tds in tHeader">
              {{ [trs,tds.key] | menuNameSet }}
            </td>
          </tr>
          </tbody>
        </table>
      </div>
      <!--表格翻页-->
        <Pages
          v-show="tableRow.elementsSum !== 0"
          v-if="tableRow"
          :showItem="10"
          :pageSize="20"
          :allDataSum="tableRow.elementsSum"
          :allpage="(Math.ceil(tableRow.elementsSum/20))"
          @current-change="tablePageData"
        ></Pages>

      <!--新增权限因子-->
      <MenuSelectModal
        :menuAddObj="menuAddObj"
        @menuAddClose="menuAddCloseFn"
      ></MenuSelectModal>

      <!--修改权限因子-->
      <MenuRevampModal
        :menuRevamp="menuRevamp"
        :jurisdictionId="jurisdictionId"
        @menuRevampClose="menuRevampCloseFn"
      ></MenuRevampModal>
    </div>
  </div>
</template>

<script>
    import Pages from "../../sudoku/atom/pages.vue"
    import MenuSelectModal from "./MenuAddModal"
    import MenuRevampModal from "./MenuRevampModal"
    export default {
        name: "jurisdictionTable",
        data(){
          return{
            isActive:-1,
            params:{
              pageNum:1,
              pageSize:20
            },
            tableRow:[],
            tHeader:[
              {"key":"name","value":"权限因子名称"},
              {"key":"privilegeFunction|name","value":"菜单名称"},
              {"key":"remark","value":"备注"}
            ],
            menuAddObj:{
              isShow:false
            },
            menuRevamp:{
              isShow:false
            },
            jurisdictionId:null
          }
        },
        filters:{
          menuNameSet(value){
            let obj = value[0];
            let str = value[1];
            let returnValue = "";
            if( str.indexOf("|") !== -1 ){
              let strArr = str.split("|");
              returnValue = obj[strArr[0]][strArr[1]];
            }else{
              returnValue = obj[str]
            }
            return returnValue;
          }
        },
        mounted(){
          //GET /priActions/queryAll
          this.getPriActionsAjax();
        },
        methods:{
          //渲染权限因子列表
          getPriActionsAjax(){
            //console.log(this.params);
            this.$axios.get(this.HOST+"/priActions/queryAll",{
              params:this.params
            })
              .then((data)=>{
                //console.log(data);
                this.tableRow = data.data;
              })
              .catch((err)=>{
                //console.log(err);
              })
          },
          //选择权限因子
          selectJurisdiction(obj,index){
            this.isActive = index;
            this.jurisdictionId = obj.id;
            //console.log(obj);
          },
          //新增弹窗关闭
          menuAddCloseFn(){
            this.menuAddObj.isShow = false;
            this.getPriActionsAjax();
          },
          //新增弹窗
          menuAddFn(){
            this.menuAddObj.isShow = true;
          },
          //修改弹窗
          menuRevampFn(){
            if( this.jurisdictionId ){
              this.menuRevamp.isShow = true;
            }else{
              this.s_alert("请选择需要修改的因子！")
              // alert("请选择需要修改的因子！")
            }
          },
          //修改弹窗关闭
          menuRevampCloseFn(){
            this.menuRevamp.isShow = false;
            this.getPriActionsAjax();
          },
          //删除功能
          menuDeleteFn(){
            if( this.jurisdictionId ){
              let msg = confirm("是否删除该因子？");
              if( msg ){
                this.$axios["delete"](this.HOST+"/priActions/deleteAction/"+this.jurisdictionId)
                  .then((data)=>{
                    //console.log(data);
                    if( data.data.success ){
                      this.s_alert("删除成功！");
                      // alert("删除成功！");
                      this.s_logwrite("基础信息管理-权限因子管理","删除");
                      //this.tableRow.splice(this.isActive,1);
                      this.isActive = -1;//清除选项
                      this.jurisdictionId = null;//清除id
                      this.getPriActionsAjax();//刷新表
                    }else {
                      this.s_alert(data.data.errorMsg)
                      // alert(data.data.errorMsg)
                    }
                  })
                  .catch((err)=>{
                    //console.log(err);
                  })
              }
            }else{
              this.s_alert("请选择需要删除的因子！");
              // alert("请选择需要删除的因子！");
            }
          },
          //翻页
          tablePageData(page){
            this.isActive = -1;
            this.jurisdictionId = null;
            this.params.pageNum = page;
            this.getPriActionsAjax();
          }
        },
        components:{
          MenuSelectModal,
          MenuRevampModal,
          Pages
        }
    }
</script>

<style scoped>

</style>
